package twitterfeed.languages;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * This class main use is to read a set of words from a xml file and put it in
 * an ArrayList
 * 
 * @version 2.0
 * 
 */
public class Language {

	private ArrayList<String> wordList = new ArrayList<String>();
	private static Language language;

	/**
	 * Constructor of Language Language is a Singleton, therefore the private
	 * constructor Opens the xml file with the languages, reads the words and
	 */
	private Language() {
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(new File("languages.xml"));
			doc.getDocumentElement().normalize();
			NodeList listOfWords = doc.getElementsByTagName("word");
			for (int i = 0; i < listOfWords.getLength(); i++) {
				wordList.add(listOfWords.item(i).getTextContent());
			}
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Returns the a static instance of Language
	 * 
	 * @return
	 */
	public static Language getLanguage() {
		if (language == null) {
			language = new Language();
		}
		return language;
	}

	/**
	 * Returns the ArrayList<String> with typical words
	 * 
	 * @return wordList
	 */
	public ArrayList<String> getTypicalWords() {
		return wordList;
	}
}
